<template>
  <el-container class="home_container">
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt="" />
        <span>黑马后台管理系统</span>
      </div>
      <el-button @click="loginout" type="info">点击退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧导航栏 -->
        <!-- :default-active="$route.path"这种方法也可以，这样就不用把url地址存起来了 -->
        <!-- 使用存储的方法的话，点击地址栏返回的时候，导航栏不会发生变化 -->
      <el-aside :width="showCollapse ? '64px' : '200px'">
        <div class="toggle" @click="hendleToggle"> | | |</div>
        <el-menu
          :unique-opened = 'true'
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse='showCollapse'
          :collapse-transition='false'
          :router='true'
          @select='hendleActive'
        >
         <!-- 一级导航菜单 -->
          <el-submenu :index="item.id + ''" v-for="(item,index) in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span class="text">{{item.authName}}</span>
            </template>
             <!-- 二级导航菜单 -->
            <el-menu-item :index="'/'+item1.path + ''" v-for="(item1,index1) in item.children" :key="item1.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item1.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
        <router-view class="view"></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

import {menus} from '../../api/menus'
export default {
  data(){
    return{
      menuList:[],
      iconObj:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      showCollapse:false,
     indexActive:''
    }
  },
  created(){
     // 获取左侧菜单栏数据
      this.getMenusData()

      // this.indexActive = window.sessionStorage.getItem('index') || ''
  },
  methods: {
    // 退出登录
    loginout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },

    // 获取左侧菜单栏数据
   async getMenusData(){
      var{data:res} =  await menus()
      // console.log(res)
      this.menuList = res.data
      // console.log(this.menuList)
    },
    // toggle切换
    hendleToggle(){
       this.showCollapse = !this.showCollapse
       window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); 
    },
    // 处理导航栏选中状态
    hendleActive(e){
      // console.log(e)
      // window.sessionStorage.setItem('index',e)
    }
      
  },
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100vh;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  .logo {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
      color: #ffffff;
      font-size: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: 0px;
  }
}
.home_container .el-main {
  background-color: #eaedf1;
 
  height: 100%;


}
.text{
  margin-left: 5px;
}
.toggle{
  width: 100%;
  line-height: 28px;
  background-color: #ccc;
  text-align: center;
  cursor: pointer;
}
</style>>

